<!--
> Muaz Khan     - github.com/muaz-khan 
> MIT License   - www.webrtc-experiment.com/licence
> Documentation - www.RTCMultiConnection.org
-->
<script>
    if(!location.hash.replace('#', '').length) {
        location.href = location.href.split('#')[0] + '#' + (Math.random() * 100).toString().replace('.', '');
        location.reload();
    }
</script>

<h1><a href="http://www.RTCMultiConnection.org/docs/">RTCMultiConnection</a> & Password Protect Rooms</h1>
<button id="setup">Setup RTC-Multi-Connection</button>
<hr />

<script src="https://cdn.webrtc-experiment.com/socket.io.js"></script>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<script src="https://cdn.webrtc-experiment.com/CodecsHandler.js"></script>
<script src="https://cdn.webrtc-experiment.com/IceServersHandler.js"></script>
<script src="https://cdn.webrtc-experiment.com/RTCMultiConnection.js"></script>
<script>
document.querySelector('#setup').onclick = function() {
    // room password has been set before calling "open" method
    connection.extra.password = prompt('Setup password for your room!');
    connection.open();
    this.disabled = true;
};

var connection = new RTCMultiConnection();

// https://github.com/muaz-khan/WebRTC-Experiment/tree/master/socketio-over-nodejs
var SIGNALING_SERVER = 'https://socketio-over-nodejs2.herokuapp.com:443/';
connection.openSignalingChannel = function(config) {
    var channel = config.channel || connection.channel || 'default-namespace';
    var sender = Math.round(Math.random() * 9999999999) + 9999999999;

    io.connect(SIGNALING_SERVER).emit('new-channel', {
        channel: channel,
        sender: sender
    });

    var socket = io.connect(SIGNALING_SERVER + channel);
    socket.channel = channel;

    socket.on('connect', function() {
        if (config.callback) config.callback(socket);
    });

    socket.send = function(message) {
        socket.emit('message', {
            sender: sender,
            data: message
        });
    };

    socket.on('message', config.onmessage);
};

var sessions = {};
connection.onNewSession = function(session) {
    if (sessions[session.sessionid]) return;
    sessions[session.sessionid] = session;

    // set password for person who is trying to join the room
    connection.extra.password = prompt('Enter password to join this room.');
    connection.join(session);
};

connection.onRequest = function(e) {
    // validating password in "onRequest"
    if (e.extra.password != connection.extra.password)
        return alert('password: ' + e.extra.password + ' !== ' + connection.extra.password);

    connection.accept(e);
};

connection.onstream = function(e) {
    document.body.appendChild(e.mediaElement);
};
connection.connect();
</script>
<pre>
document.querySelector('#setup').onclick = function() {
    // room password has been set before calling "open" method 
    connection.extra.password = prompt('Setup password for your room!');
    connection.open();
};
connection.onNewSession = function(session) {
    // set password for person who is trying to join the room 
    connection.extra.password = prompt('Enter password to join this room.');
    connection.join(session);
};
connection.onRequest = function(userid, extra) {
    // validating password in "onRequest" 
    if (extra.password != connection.extra.password)
        return alert('password: ' + extra.password + ' !== ' + connection.extra.password);
    connection.accept(userid, extra);
};
</pre>
<hr />
<style>
html {
    background: rgb(255, 252, 239);
}

audio,
video {
    vertical-align: top;
    width: 40%;
}
</style>